﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using MyWebPagesStarterKit.Controls;

public partial class Administration_Roles : PageBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        gwRoles.DataSource = null;
        gwRoles.DataSource = Roles.GetAllRoles();
        gwRoles.DataBind();
    }

    protected void Action(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "tab1":
                multiview.SetActiveView(view1);
                t1.CssClass = "default-tab";
                t2.CssClass = "";
                t3.CssClass = "";
                break;
            case "tab2":
                multiview.SetActiveView(view2);
                t1.CssClass = "";
                t2.CssClass = "default-tab";
                t3.CssClass = "";
                break;
            case "tab3":
                multiview.SetActiveView(view3);
                t1.CssClass = "";
                t2.CssClass = "";
                t3.CssClass = "default-tab";
                lstRoles.Items.Clear();
                SetupUsers();
                 break;
            case "create_role":
                CreateRole();
                break;
            case "delete_role":
                string role = (string)e.CommandArgument;
                if (Roles.GetUsersInRole(role).Length == 0)
                    Roles.DeleteRole(role);
                break;
            case "edit_role":
                string editrole = (string)e.CommandArgument;
                multiview.SetActiveView(view2);
                t1.CssClass = "";
                t2.CssClass = "default-tab";
                t3.CssClass = "";
                txtRole.Text = editrole;
                break;
        }
        LoadData();
    }

    private void CreateRole()
    {
        if (txtRole.Text != string.Empty && (!Roles.RoleExists(txtRole.Text)))
            Roles.CreateRole(txtRole.Text);
    }

    private void SetupUsers()
    {
        lstUsers.DataSource = Membership.GetAllUsers();
        lstUsers.DataBind();
    }

    protected void UpdateRoles(object sender, EventArgs e)
    {
        lstRoles.Items.Clear();

        if (!string.IsNullOrEmpty(lstUsers.SelectedValue))
        {
            string[] roles = Roles.GetAllRoles();

            MembershipUser user = Membership.GetUser(lstUsers.SelectedValue);

            if (user != null)
            {
                foreach (string role in roles)
                {
                    ListItem item = new ListItem(role, role);
                    lstRoles.Items.Add(item);
                    if (Roles.IsUserInRole(user.UserName, role))
                        item.Selected = true;
                    else
                        item.Selected = false;
                }
            }
        }
    }

    protected void UpdateUserRoles(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser(lstUsers.SelectedValue);
        foreach (ListItem item in lstRoles.Items)
        {
            if (item.Selected == true)
            {
                if (!Roles.IsUserInRole(user.UserName, item.Value))
                    Roles.AddUserToRole(user.UserName, item.Value);
            }
            else
            {
                if (Roles.IsUserInRole(user.UserName, item.Value))
                {
                    if (item.Value == MyWebPagesStarterKit.RoleNames.Administrators.ToString())
                    {
                        if (Roles.GetUsersInRole(MyWebPagesStarterKit.RoleNames.Administrators.ToString()).Length > 1)
                            Roles.RemoveUserFromRole(user.UserName, item.Value);
                    }
                    else
                    {
                        Roles.RemoveUserFromRole(user.UserName, item.Value);
                    }
                }
            }
        }
    }
}
